package com.dao;

import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.bean.car;

public class carDao {
    // 添加车辆信息
    public static void addcar(car car) {
        Connection conn = null;
        PreparedStatement pStmt = null;
        try {
            conn = baseDao.getConnection();
            String sql = "INSERT INTO car(img, name, firm, type, oil, price) VALUES (?, ?, ?, ?, ?, ?)";
            pStmt = conn.prepareStatement(sql);
            pStmt.setString(1, car.getImg());
            pStmt.setString(2, car.getName());
            pStmt.setString(3, car.getFirm());
            pStmt.setString(4, car.getType());
            pStmt.setString(5, car.getOil());
            pStmt.setString(6, new String(car.getPrice().getBytes("UTF-8"), "UTF-8"));
            pStmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        } finally {
            baseDao.closeAll(null, pStmt, conn);
        }
    }

    // 删除车辆信息
    public static void deletecar(String name) {
        Connection conn = null;
        PreparedStatement pStmt = null;
        try {
            conn = baseDao.getConnection();
            String sql = "DELETE FROM car WHERE name=?";
            pStmt = conn.prepareStatement(sql);
            pStmt.setString(1, name);
            pStmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            baseDao.closeAll(null, pStmt, conn);
        }
    }

    // 修改车辆信息
    public static void updatecar(car car) {
        Connection conn = null;
        PreparedStatement pStmt = null;
        try {
            conn = baseDao.getConnection();
            String sql = "UPDATE car SET img=?, firm=?, type=?, oil=?, price=? WHERE name=?";
            pStmt = conn.prepareStatement(sql);
            pStmt.setString(1, car.getImg());
            pStmt.setString(2, car.getFirm());
            pStmt.setString(3, car.getType());
            pStmt.setString(4, car.getOil());
            pStmt.setString(5, car.getPrice());
            pStmt.setString(6, car.getName());
            pStmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            baseDao.closeAll(null, pStmt, conn);
        }
    }

    // 查询所有车辆信息
    public static List<car> getAllcars() {
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        List<car> allcars = new ArrayList<>();
        try {
            conn = baseDao.getConnection();
            stmt = conn.prepareStatement("SELECT * FROM car");
            rs = stmt.executeQuery();
            while (rs.next()) {
                car car = new car(
                        rs.getString("img"),
                        rs.getString("name"),
                        rs.getString("firm"),
                        rs.getString("type"),
                        rs.getString("oil"),
                        rs.getString("price")
                );
                allcars.add(car);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            baseDao.closeAll(rs, stmt, conn);
        }
        return allcars;
    }

    // 根据车辆名称查询单个车辆信息
    public static car getcarByName(String name) {
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        car car = null;
        try {
            conn = baseDao.getConnection();
            stmt = conn.prepareStatement("SELECT * FROM car WHERE name=?");
            stmt.setString(1, name);
            rs = stmt.executeQuery();
            if (rs.next()) {
                car = new car(
                        rs.getString("img"),
                        rs.getString("name"),
                        rs.getString("firm"),
                        rs.getString("type"),
                        rs.getString("oil"),
                        rs.getString("price")
                );
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            baseDao.closeAll(rs, stmt, conn);
        }
        return car;
    }
}
